package com.example.demo.swj.uitl;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.Charset;

/**
 * @author baidh
 * @date 2020/08/31
 */
public class EncryptUtil {

    public static String aesEncrypt(String encryptKey, String content, String chanrset) {
        return SecureUtil.aes(SecureUtil.decode(encryptKey)).encryptBase64(content, Charset.forName(chanrset));
    }

    public static String aesDecrypt(String encryptKey, String content, String chanrset) {
        return SecureUtil.aes(SecureUtil.decode(encryptKey)).decryptStr(content, Charset.forName(chanrset));
    }

//    public static String sm4Encrypt(String encryptKey, String content, String chanrset) {
//        return new String(new SymmetricCrypto("SM4", Base64.decode(encryptKey)).encrypt(content, Charset.forName(chanrset)), Charset.forName(chanrset));
//    }
//
//    public static String sm4Decrypt(String encryptKey, String content, String chanrset) {
//        return new SymmetricCrypto("SM4", Base64.decode(encryptKey)).decryptStr(content, Charset.forName(chanrset));
//    }

    public static String sm4Encrypt(String key, String content, String chanrset) {
        NSymmetricCrypto sm4 = SmUtils.sm4(HexUtil.decodeHex(key));
        return sm4.encryptHex(content, Charset.forName(chanrset));
    }

    public static String sm4Decrypt(String key, String encryptHex, String chanrset) {
        NSymmetricCrypto sm4 = SmUtils.sm4(HexUtil.decodeHex(key));
        return sm4.decryptStr(encryptHex, Charset.forName(chanrset));
    }

}
